package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.FeedbackMapper;
import com.itheima.pojo.Comment;
import com.itheima.pojo.Feedback;
import com.itheima.pojo.FeedbackVO;
import com.itheima.service.FeedbackService;
import com.itheima.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class FeedbackServiceImpl implements FeedbackService {

    @Autowired
    private FeedbackMapper feedbackMapper;

    @Autowired
    private IdWorker idWorker;

    /**
     * 分页+条件查询
     * @param searchMap
     * @return
     */
    @Override
    public PageInfo getList(FeedbackVO feedbackVO) {
        Integer currentPage = feedbackVO.getCurrentPage();
        Integer pageSize = feedbackVO.getPageSize();
        PageHelper.startPage(currentPage,pageSize);
        Example example = createExample(feedbackVO);
        List<Feedback> feedbacks = feedbackMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo(feedbacks);
        return pageInfo;
    }

    /**
     * 删除选中
     * @param searchList
     */
    @Override
    public void deleteBatch(FeedbackVO feedbackVO) {
        Example example = createExample(feedbackVO);
        feedbackMapper.deleteByExample(example);
    }

    /**
     * 编辑
     * @param searchMap
     */
    @Override
    public void edit(FeedbackVO feedbackVO) {
        Feedback feedback = JSON.parseObject(JSON.toJSONString(feedbackVO), Feedback.class);
        //根据uid查询需要编辑的对象
        Example example = new Example(Comment.class);
        Example.Criteria criteria = example.createCriteria();
        if(feedbackVO!=null) {
            if (feedbackVO.getUid() != null && !"".equals(feedbackVO.getUid())) {
                criteria.andEqualTo("uid", feedbackVO.getUid());
            }
        }
        feedbackMapper.updateByExample(feedback,example);
    }

    //提交反馈
    @Override
    public int addFeedback(FeedbackVO feedbackVO) {
        Feedback feedback = new Feedback();
        feedback.setUid(idWorker.nextId()+"");
        feedback.setUserUid(feedbackVO.getUserUid());
        feedback.setTitle(feedbackVO.getTitle());
        feedback.setContent(feedbackVO.getContent());
        feedback.setReply(feedbackVO.getReply());
        feedback.setFeedbackStatus(feedbackVO.getFeedbackStatus());
        feedback.setStatus(1);
        return feedbackMapper.insert(feedback);
    }

    private Example createExample(FeedbackVO feedbackVO) {
        Example example=new Example(Feedback.class);
        Example.Criteria criteria = example.createCriteria();
        if(feedbackVO!=null) {
            // 评论内容
            if (feedbackVO.getContent() != null && !"".equals(feedbackVO.getContent())) {
                criteria.andEqualTo("content", feedbackVO.getContent());
            }
            // 回复
            if (feedbackVO.getReply() != null && !"".equals(feedbackVO.getReply())) {
                criteria.andEqualTo("reply", feedbackVO.getReply());
            }
            // 状态
            if (feedbackVO.getStatus() != null && !"".equals(feedbackVO.getStatus())) {
                criteria.andEqualTo("status", feedbackVO.getStatus());
            }
            // 标题
            if (feedbackVO.getTitle() != null && !"".equals(feedbackVO.getTitle())) {
//                criteria.andEqualTo("title", feedbackVO.getTitle());
                criteria.andLike("title", "%"+feedbackVO.getTitle()+"%");
            }
            // 唯一id
            if (feedbackVO.getUid() != null && !"".equals(feedbackVO.getUid())) {
                criteria.andEqualTo("uid", feedbackVO.getUid());
            }
            // 用户uid
            if (feedbackVO.getUserUid() != null && !"".equals(feedbackVO.getUserUid())) {
                criteria.andEqualTo("userUid", feedbackVO.getUserUid());
            }
            // 反馈状态
            if (feedbackVO.getFeedbackStatus() != null && !"".equals(feedbackVO.getFeedbackStatus())) {
                criteria.andEqualTo("feedbackStatus", feedbackVO.getFeedbackStatus());
            }
        }
        return example;
    }
}
